package com.xiayong.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Character encoding filter.
 * 
 * @author XiaYong
 * 
 */
public class CharsetFilter implements Filter {
    private static String ENCODING;

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
            final FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(CharsetFilter.ENCODING);
        servletResponse.setCharacterEncoding(CharsetFilter.ENCODING);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(final FilterConfig config) throws ServletException {
        CharsetFilter.ENCODING = config.getInitParameter("encoding");
    }

}
